package com.springcloud.controller;

import com.springcloud.pojo.Dept;

import com.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ProjectName: spring_cloud_demo
 * @Package: com.com.springcloud.controller
 * @ClassName: DeptController
 * @Author:
 * @Description:
 * @Date: 2020/10/13 9:05
 * @Version: 1.0
 */

@RestController
//@RequestMapping("/dept")
public class DeptController {


    @Autowired
    DeptService deptService;

    @Autowired
    DiscoveryClient client;  //后面模块用到的



    @PostMapping("/dept/add")
//    @GetMapping("/dept/add")
    public Boolean addDept(Dept dept) {
        return deptService.addDept(dept);
//        return this.deptClientService.addDept(dept);
    }

    @GetMapping("/dept/QueryAll")
    public List<Dept> QueryAll() {
        return deptService.QueryAll();
//        return this.deptClientService.QueryAll();
    }

    @GetMapping("/dept/get/{id}")
    public Dept getById(@PathVariable("id") Long id) {
        Dept dept = new Dept();
        dept = deptService.getById(id);
        System.out.println(dept);
        return dept;
    }

    /**
     * 注册进来的微服务 获取一些消息   后面模块用到的
     *
     * @return
     */
    @GetMapping("/dept/discovery")
    public Object discovery() {
        List<String> list = client.getServices();

        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost() + "\t" +
                    instance.getPort() + "\t" +
                    instance.getUri() + "\t" +
                    instance.getServiceId());
        }
        return this.client;
    }
}
